<template>
    <div style="width: 100%;">
    <div class="gva-search-box" style="padding: 16px; border-radius: 4px; margin-bottom: 16px; width: 100%;">
      <el-form ref="elSearchFormRef" :inline="true" :model="searchInfo" class="demo-form-inline">
        <el-form-item label="会员姓名" v-if="!memberName">
          <el-input v-model="searchInfo.memberName" placeholder="请输入会员姓名" clearable style="width: 180px;" />
        </el-form-item>

        <el-form-item label="手机号" v-if="!phone">
          <el-input v-model="searchInfo.phone" placeholder="请输入手机号" clearable style="width: 180px;" />
        </el-form-item>

        <el-form-item label="时间范围">
          <el-date-picker
            v-model="searchInfo.timeRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            clearable
            style="width: 250px;"
          />
        </el-form-item>

        <el-form-item label="支付方式">
          <el-select v-model="searchInfo.paymentMethod" placeholder="全部" style="width: 120px;" clearable>
            <el-option label="全部" value="" />
            <el-option label="现金" :value="1" />
            <el-option label="微信支付" :value="2" />
            <el-option label="支付宝" :value="3" />
            <el-option label="银行卡" :value="4" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" @click="onSubmit" style="margin-left: 10px;">查询</el-button>
          <el-button type="success" @click="exportRecords" style="margin-left: 10px;" icon="Download">导出</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="gva-table-box" style="width: 100%;">
      <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px;">
        <h3 style="margin: 0; color: #303133;">充值记录</h3>
        <div style="color: #909399; font-size: 14px;">
          共 {{ total }} 条记录
        </div>
      </div>

      <el-table
        style="width: 100%"
        tooltip-effect="dark"
        :data="tableData"
        row-key="id"
      >
        <el-table-column align="left" label="单号" prop="orderNo" width="200">
          <template #default="scope">
            <div style="font-family: monospace; font-size: 12px;">{{ scope.row.orderNo }}</div>
          </template>
        </el-table-column>

        <el-table-column align="left" label="会员信息" min-width="180">
          <template #default="scope">
            <div>
              <div style="font-weight: 500;">{{ scope.row.memberName }}</div>
              <div style="color: #909399; font-size: 12px;">{{ scope.row.phone }}</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column align="left" label="充值金额" prop="rechargeAmount" width="140">
          <template #default="scope">
            <span style="color: #67c23a; font-weight: 500;">¥{{ scope.row.rechargeAmount.toFixed(2) }}</span>
          </template>
        </el-table-column>

        <el-table-column align="left" label="赠送金额" prop="giftAmount" width="140">
          <template #default="scope">
            <span v-if="scope.row.giftAmount > 0" style="color: #e6a23c; font-weight: 500;">+¥{{ scope.row.giftAmount.toFixed(2) }}</span>
            <span v-else style="color: #e6a23c; font-weight: 500;">¥0</span>
          </template>
        </el-table-column>

        <el-table-column align="left" label="到账金额" prop="finalAmount" width="140">
          <template #default="scope">
            ¥{{ scope.row.finalAmount.toFixed(2) }}
          </template>
        </el-table-column>

        <el-table-column align="left" label="支付方式" prop="paymentMethod" width="120">
          <template #default="scope">{{ formatPaymentMethod(scope.row.paymentMethod) }}</template>
        </el-table-column>

        <el-table-column align="left" label="操作员" prop="operatorName" width="120">
          <template #default="scope">{{ scope.row.operatorName || '系统' }}</template>
        </el-table-column>

        <el-table-column align="left" label="充值时间" prop="rechargeTime" width="180">
          <template #default="scope">{{ formatDate(scope.row.rechargeTime) }}</template>
        </el-table-column>

        <el-table-column align="left" label="备注" prop="remark" min-width="200" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.remark || '' }}</template>
        </el-table-column>
      </el-table>

      <div class="gva-pagination" style="margin-top: 16px;">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="page"
          :page-size="pageSize"
          :page-sizes="[10, 30, 50, 100]"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { getMembersRechargeList } from '@/api/hairmember/members'
import { formatDate } from '@/utils/format'
import { ElMessage } from 'element-plus'
import { ref, reactive, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { Download } from '@element-plus/icons-vue'

defineOptions({
  name: 'RechargeRecords'
})

const route = useRoute()
const memberId = route.query.memberId
const memberName = route.query.memberName

// 搜索条件
const searchInfo = ref({
  memberName: memberName || '',
  phone: route.query.phone || '',
  paymentMethod: '',
  timeRange: []
})

// 表格数据
const tableData = ref([])
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const totalAmount = ref(0)

// 格式化支付方式
const formatPaymentMethod = (method) => {
  const methods = {
    1: '现金',
    2: '微信支付',
    3: '支付宝',
    4: '银行卡'
  }
  return methods[method] || '未知'
}

// 获取充值记录
const getTableData = async () => {
  try {
    const params = {
      page: page.value,
      pageSize: pageSize.value,
      ...searchInfo.value
    }
    params.timeRange = null

    // 如果有时间范围，处理时间参数
    if (searchInfo.value.timeRange && searchInfo.value.timeRange.length === 2) {
      params.startDate = searchInfo.value.timeRange[0]
      params.endDate = searchInfo.value.timeRange[1]
    }

    if (memberId) {
      params.memberId = memberId
    }

    const res = await getMembersRechargeList(params)
    if (res.code === 0) {
      tableData.value = res.data.list
      total.value = res.data.total
      page.value = res.data.page
      pageSize.value = res.data.pageSize
      totalAmount.value = res.data.totalAmount || 0
    }
  } catch (error) {
    ElMessage.error('获取充值记录失败')
  }
}

// 搜索
const onSubmit = () => {
  page.value = 1
  getTableData()
}

// 重置
const onReset = () => {
  searchInfo.value = {
    memberName: memberName || '',
    phone: route.query.phone || '',
    paymentMethod: '',
    timeRange: []
  }
  getTableData()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

// 导出记录
const exportRecords = () => {
  ElMessage.success('导出功能开发中...')
}

// 初始化
onMounted(() => {
  getTableData()
})
</script>

<style scoped>
.recharge-records {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 16px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.page-title {
  margin: 0;
  color: #303133;
  font-size: 18px;
  font-weight: 500;
}

.page-stats {
  color: #909399;
  font-size: 14px;
}

.amount-positive {
  color: #67c23a;
  font-weight: 500;
}

.amount-gift {
  color: #e6a23c;
  font-weight: 500;
}

.amount-total {
  color: #303133;
  font-weight: 600;
}
</style>